#ifndef DRIVERJACK_H
#define DRIVERJACK_H
//
#include <jack/jack.h>
#include <QStringList>
#include <QSharedPointer>
#include <QThread>
#include "free-cube/adriver.h"
#include "free-cube/buffer.h"


class QString;
class QWaitCondition;

namespace fc {
typedef int ReturnCode;
typedef fc::AudioT Sample;
const char AUDIO_OUT_PORT_PREFIX[] = "aOut%1";
const char AUDIO_IN_PORT_PREFIX[] =  "aIn%1";
const char MIDI_OUT_PORT_PREFIX[] =  "mOut%1";
const char MIDI_IN_PORT_PREFIX[] =  "mIn%1";
const char NAME_OF_DRIVER_JACK[] = "METRONOME";
// 32 audio in and outs, 32 midi in and outs
const int MAX_N_PORTS = 32;
const int N_DIGITS_IN_A_PORT_NAME = 2;
}

class DriverJack : public fc::ADriver
{
public:

    DriverJack(const char * nameOfClient = fc::NAME_OF_DRIVER_JACK);
    ~DriverJack();

    int buffer_size();
    int sample_rate();
    void set_buffer(fc::Buffer & buffer){buffer_ = buffer;}
    QWidget * control_panel(){return NULL;}
    const QString & name();
    int init(fc::AHostHandler & host_handler); // set all prerequisites
    void  start();
    void  stop();


private:    


    QStringList * getPhysicalPortNames(bool isJackOutput, bool isAudio);
    QStringList * getMyPortNames();
    QString setPortName(int portID, const QString & portName);
    int	registerPort(fc::Sample * buffer,
                     bool isOutput, QString name = QString(""));
    fc::ReturnCode deletePort(int portID);
    fc::ReturnCode clearPorts();
    fc::ReturnCode connectDefaultPorts();
    fc::ReturnCode connectPort(const int myPortID, const QString & extPortName);
    fc::ReturnCode disconnectPort(const QString & myPortName,
                              const QString & extPortName);


    fc::ReturnCode stopJack(); // stop process audio
    fc::ReturnCode startJack();// start process audio

    enum DriverState {CREATED, INITED, STARTED, STOPED} driverState_;

    QString nameOfClient_;
    const QString nameOfDriver_;
    struct StaticData;
    struct AudioPortStruct;
    static StaticData staticData_;
    fc::Buffer buffer_;

    class HostThread;
    HostThread * host_thread_;

    //QThread ovverloading function
};
#endif
